package com.lanmaoly;

/**
 * 指令重排序
 *
 * @author Administrator
 */
public class Intest {

    private static int num = 0;
    private static boolean ready = false;

    public static class ReadThread extends Thread {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                if (ready) {
                    int i = num + num;
                    System.out.println(i);
                    if (i == 0) {
                        System.out.println("zero .......");
                    }
                }
            }
        }
    }


    public static class WriteThread extends Thread {
        @Override
        public void run() {
            num = 2;
            ready = true;
            System.out.println("WriteThread set over...");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReadThread readThread = new ReadThread();
        readThread.start();

        WriteThread writeThread = new WriteThread();
        writeThread.start();

        Thread.sleep(10);
        readThread.interrupt();

        System.out.println("main exit...");
    }

}


